package cn.smart.epic.core.support;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther Uncle_Z
 * @date 2017/9/19 22:52
 * @Email:2545375@qq.com
 * @Descrition
 * @since 1.0
 */

public class WafRequestWrapper extends HttpServletRequestWrapper {

    private boolean filterXSS = true;
    private boolean filterSQL = true;

    public WafRequestWrapper(HttpServletRequest request, boolean filterSQL, boolean filterXSS) {
        super(request);
        this.filterSQL = filterSQL;
        this.filterXSS = filterXSS;
    }

    public WafRequestWrapper(HttpServletRequest request) {
        this(request, true, true);
    }

    @Override
    public String[] getParameterValues(String name) {
        String[] values = super.getParameterValues(name);
        if (values == null) {
            return null;
        }

        int count = values.length;
        String[] encodeValues = new String[count];

        for (int i = 0; i < count; i++) {
            encodeValues[i] = filterParamString(values[i]);
        }

        return encodeValues;
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        Map<String, String[]> primary = super.getParameterMap();
        Map<String, String[]> result = new HashMap<>(primary.size());
        for (Map.Entry<String, String[]> stringEntry : primary.entrySet()) {
            result.put(stringEntry.getKey(), filterEntryString(stringEntry.getValue()));
        }
        return result;
    }


    protected String[] filterEntryString(String[] rawValue) {
        int count = rawValue.length;
        String[] tmpValues = new String[count];
        for (int i = 0; i < count; i++) {
            tmpValues[i] = filterParamString(rawValue[i]);
        }
        return tmpValues;
    }

    /**
     * 参数过滤
     * @param name
     * @return
     */
    @Override
    public String getParameter(String name) {
        return filterParamString(super.getParameter(name));
    }

    /**
     * 请求头过滤
     * @param name
     * @return
     */
    @Override
    public String getHeader(String name) {
        return filterParamString(super.getHeader(name));
    }

    /**
     * Cookie内容过滤
     * @return
     */
    @Override
    public Cookie[] getCookies() {
        Cookie[] existingCookies = super.getCookies();
        if (existingCookies != null) {
            for (int i = 0 ; i < existingCookies.length ; ++i) {
                Cookie cookie = existingCookies[i];
                cookie.setValue(filterParamString(cookie.getValue()));
            }
        }
        return existingCookies;
    }

    /**
     * 过滤字符串内容
     *
     * @param rawValue
     * @return
     */
    protected String filterParamString(String rawValue) {
        if (rawValue == null) {
            return null;
        }
        String tmpStr = rawValue;
        if (this.filterXSS) {
            tmpStr = WafKit.stripXSS(rawValue);
        }
        if (this.filterSQL) {
            tmpStr = WafKit.stripSqlInjection(tmpStr);
        }
        return tmpStr;
    }
}
